function handles = chooseImageFile(handles)
% chooseImageFile
%
% See also addSliceImage
%
% Editing:
% gs april 2005
% ---------------------------------------------------

% TODO: need to delete the previous images and markers (if any),
% rather than add them to the axes
% ---------------------------------------------------------------

% specify the default directory
d = handles.data.analysis.uncagingPathName;
inds = strfind(d, '\');
try
    lastchar = inds(length(inds) - 2); % 'standard' auto-generated filing system, old way
    imgDir = [d( 1 : lastchar ) 'images'];
    cd(imgDir);
catch
    try
        lastchar = inds(length(inds) - 1); % 'standard' auto-generated filing system, new way
        imgDir = [d( 1 : lastchar ) 'images'];
        cd(imgDir);
    catch
    end
end

% interactively select the file to open
[name, path] = uigetfile('*.*', 'Select an image file.');
if isnumeric(name)
    return
end
fullname = [path name];

% read in the image file & convert to RGB format
R = imread(fullname);

% store some info
handles.data.image.imgDir = imgDir;
handles.data.image.imgName = name;
handles.data.image.img = R;
handles.data.image.info = imfinfo(fullname);

% display image
figure('Color', 'w', 'UserData', handles);
colormap(gray(256));

% flip vertically, for display, so positive is up
for n = 1:size(R,3)
    R(:,:,n) = flipud(R(:,:,n));
end
% hSliceImg = imagesc(flipud(R));
hSliceImg = imagesc(R);

set(gca, 'YDir', 'normal');
daspect([1 1 1]);

% set(himg, 'ButtonDownFcn', 'addSliceImage');
% set(gca, 'ButtonDownFcn', 'chooseImageFile');
% title('Click axes to upload an image');

% distance scaling
try
    xrange = handles.data.acq.imageXrange;
    yrange = handles.data.acq.imageYrange;
catch
    xrange = 1900; % default param's from Ingrid's rig
    yrange = 1520;
end
set(hSliceImg, 'XData', [-xrange/2 xrange/2], 'YData', [-yrange/2 yrange/2]);
axis tight;

% gs 2008 03 08 -- add menu for distance measurement:
addMenu4DistanceMeasure;


% handleList = [handles.data.analysis.haxSliceImg_currentTrace; ...
%     handles.data.analysis.hRectMapArea; handles.data.analysis.hBlankMap; ...
%     handles.data.analysis.hSliceImg];
% set(data.handles.haxSliceImg, 'Children', handleList); 

% % blank map array
% hold on;
% himgBlank = imagesc(zeros(16,16));
% set(himgBlank, 'AlphaData', .1, 'XData', [-data.map.magFacX data.map.magFacX], ...
%     'YData', [-data.map.magFacY data.map.magFacY]);
% set(data.handles.haxSliceImg, 'Position', [0.4919    0.7054    0.2840    0.2555], ...
%     'ButtonDownFcn', 'addSliceImage');
% daspect([1 1 1]);
% axis tight;
% pixTrace(himgBlank);

% % rectangle to mark mapping region
% hrect = rectangle;
% xdata = get(data.handles.himgP1, 'XData');
% ydata = get(data.handles.himgP1, 'YData');
% set(hrect, 'Position', [xdata(1) ydata(1) xdata(2)-xdata(1) ydata(2)-ydata(1)], ...
%     'EdgeColor', 'y', 'LineStyle', ':');

% set(gcf, 'UserData', data);